package main

import (
	"fmt"
	"sync"
	"time"
)

var mutex sync.Mutex //创建一个互斥锁，新建的互斥锁状态为0-未加锁状态.锁只有一把

func main() {
	go person1()
	go person2()

	for {
		;
	}
}

func printer(str string) {
	//fmt.Println("foo")
	mutex.Lock()
	for _, ch := range str {
		fmt.Printf("%c", ch)
		time.Sleep(time.Millisecond * 300)
	}
	mutex.Unlock()
}

func person1() {
	printer("hello")
}

func person2() {
	printer("world")
	printer("bar")
}
